var moment = require('moment');
var md5 = require('md5');
var db = require('../config/mongo_database');
var logger = require('../config/log4js').logger('access');
var redisClient = require('../config/redis_database').redisClient;
var log = require('../route/log');

// 提现
exports.tixian = function(req, res) {
	var userid   = tokenManager.getUserId(req);
	var btype    = req.body.btype  || null;    // 币种类别
	var asset    = req.body.asset  || 0;    // 币种类别
	var tradepwd = req.body.tradepwd || '';  // 交易密码

	db.userModel.findOne({_id: userid, tradepwd:md5(tradepwd)}, function(err, user){
		if(err){
			logger.error("未查询到db.userModel.findOne({_id: userid}", userid)
			log.addLog(userid, moment(new Date()).format("HHmmss")+'error'+tradepwd, null)
			res.json({status:404, msg:'交易密码错误，累计三次我们将短信通知到您'});
		}else{
			btype = Number(btype)
			if(user.wallet[btype].asset > asset){
				user.wallet[btype].asset = user.wallet[btype].asset - asset
				option(user, btype, asset, res)
			}else{
				res.json({status:504, msg:'已超出账户可提现余额'});
			}
		}

	})
};


// 后台提现操作确认
exports.btixian = function(req, res){
	var userid   = tokenManager.getUserId(req);
	var id       = req.body._id  || '';    // 操作ID

	db.propertyModel.update({_id:id}, {$set:{trade:'2'}}, function(err){
		if(err){
			return res.json({status:500, msg:'操作失败'});
		}else{
			// 更新对账单记录
			balance.addBalance({userid:user._id, username:user.username, btype:btype, pay:(-1)*asset})
			return res.json({status:200, msg:'操作成功'});
		}
	})


	

}

// 提现操作
function option(user, btype, asset, res){
	db.userModel.update({_id:user._id}, {$set:{wallet:user.wallet}}, function(err){
		if(err){
			return res.json({status:502, msg:'提现失败'});
		}else{

			// 更新我的账单
			property.addProperty({
				userid:  user._id,
				type:    '2',
				btype:   btype,
				pay:     (-1)*asset,
				content: '账户提现',
				trade:   '1',
				detail:  {nick:user.nick}
			})
			return res.json({status:200, msg:'提现申请成功'});
		}
	})
};


//个人信息查看
exports.searchSelfInfo = function(req,res){
	var id   = tokenManager.getUserId(req);
	db.userModel.findOne({_id:id}, function(err, model){
		res.json(model); 
	});
};


//个人信息修改
exports.updateSelfInfo = function (req,res){
	var id   = tokenManager.getUserId(req);
	
	var  username = req.body.username || ''; //手机号码
    var  password = req.body.password || ''; //密码
    var  header = req.body.header || ''; //头像
    var  nick = req.body.nick || '';//昵称

    db.userModel.userModel({_id:id}, function (err, model) {
		if(err) res.sendStatus(500); 
		if(null!=model){
			db.userModel.update({_id:id}, {$set:{username:username,password:password,header:header,tradepwd:tradepwd,nick:nick}}, function(err, model){ // 执行变更
				res.sendStatus(200); 
			})
		}
	});

};


//登录密码修改
exports.updateSetPwd = function(req, res){
	var username = req.body.username;
	var password = req.body.password || ''; //密码
	var smscode  = req.body.smscode  || ''; //验证code
	var id   = tokenManager.getUserId(req);

	redisClient.get(smscode, function (err, code) {
		if(smscode!=code){
			return res.json({status:500, msg:'验证码输入错误'});
		}else{
			db.userModel.update({_id:id}, {$set:{password:md5(password)}}, function(err,){ // 执行变更
				return res.json({status:200, msg:'修改登录密码成功'});
			})
		}
	})
}



//交易密码修改
exports.updateSelfInfo = function (req,res){
	var id   = tokenManager.getUserId(req);
	var oldtradepwd   = req.body.oldtradepwd;
	var tradepwd      = req.body.tradepwd || '';//交易密码
	db.userModel.findOne({_id:id, tradepwd:md5(tradepwd)}, function (err, model) {
			if(err) return res.json({status:500, msg:'交易密码输入错误'});
			if(null!=model){
				db.userModel.update({_id:id}, {$set:{tradepwd:md5(tradepwd)}}, function(err,){ // 执行变更
					return res.json({status:200, msg:'修改交易密码成功'});
				})
			}
		});
};
